﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using QLHD.Models;
using System.Globalization;
using System.Web.Services;

namespace QLHD.WS
{
    public partial class GroupsService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(enableSession: true)]
        public static object GetList(Search obj)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            List<Group> lst = new List<Group>();
            string keyword = obj.Keyword;
            int page = int.Parse(obj.page);
            int pagesize = int.Parse(obj.pageSize);
            int skip = int.Parse(obj.skip);
            int take = int.Parse(obj.take);
            int total = 0;


            //lstUser = db.Users.OrderByDescending(q => q.ID).Skip((page - 1) * pagesize).Take(pagesize).ToList();
            //total = db.Users.Count();

            string query = "SELECT * FROM  ( SELECT Groups.*, ROW_NUMBER() OVER(ORDER BY ID DESC) AS RowNumber FROM Groups) temp WHERE temp.RowNumber BETWEEN @startRowNumber AND @endRowNumber";
            string condition = "";

            query = string.Format(query, condition);

            //check quyen xem hop dong thuoc phong nao
            SqlParameter startRowNumber = new SqlParameter("startRowNumber", (page - 1) * pagesize);
            SqlParameter endRowNumber = new SqlParameter("endRowNumber", page * pagesize);
            lst = db.ExecuteStoreQuery<Group>(query, startRowNumber, endRowNumber).ToList<Group>();
            total = lst.Count;

            DateTime now = DateTime.Now;
            int i = 1;
            return new { lst, total = total };
        }
        [WebMethod]
        public static void DoCreate(IEnumerable<GroupsUI> obj)
        {
            QLHD2013Entities db = new QLHD2013Entities();

            Group newobj = new Group();

            foreach (GroupsUI item in obj)
            {
                newobj.Name = item.Name;
                newobj.Code = item.Code;
                newobj.Description = item.Description;
                db.AddToGroups(newobj);
            }
            db.SaveChanges();
        }

        [WebMethod]
        public static void DoUpdate(IEnumerable<GroupsUI> obj)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            foreach (GroupsUI item in obj)
            {
                int id = int.Parse(item.ID);

                Group newGroup = db.Groups.Where(q => q.ID == id).FirstOrDefault();
                if (newGroup != null)
                {
                    //newhd.MaHD = obj.MaHD;
                    newGroup.Name = item.Name;
                    newGroup.Code = item.Code;
                    newGroup.Description = item.Description;
                }
            }
            db.SaveChanges();
        }

        [WebMethod]
        public static void DoDelete(int ID)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            int id = ID;
            var objGroup = db.Groups.Where(q => q.ID == id).FirstOrDefault();
            if (objGroup != null)
            {
                db.DeleteObject(objGroup);
                //db.Users.DeleteObject();
                // db.SaveChanges();
            }
        }
    }
}